package cc.mcggdd.leah.boot.modules.stock.controller;

import cn.hutool.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/stock/quotes")
public class QuotesController {
    @GetMapping("/query")
    public Map<String, Double> queryQuotes(@RequestParam("code") String code) {
        if (code.startsWith( "30") || code.startsWith( "00" ) || code.startsWith( "12" )) {
            code = "sz" + code;
        }else {
            code = "sh" + code;
        }
        String Url = "http://hq.sinajs.cn/list=" + code;
        String response = HttpRequest.get(Url).header("Referer", "https://finance.sina.com.cn").execute().body();
        String[] items = response.split(",");
        Map<String, Double> result = new HashMap<>();
        result.put("now_price", Double.parseDouble(items[3]));
        result.put("open_price", Double.parseDouble(items[1]));
        result.put("close_price", Double.parseDouble(items[2]));
        result.put("low", Double.parseDouble(items[5]));
        result.put("high", Double.parseDouble(items[4]));
        return result;
    }
}
